package com.crm.service.repositoryService;

import com.crm.dao.client.ClientInfoDao;
import com.crm.dao.client.ContactDao;
import com.crm.dao.permission.UserInfoDAO;
import com.crm.dao.purchaseDAO.PurchaseDAO;
import com.crm.dao.purchaseDAO.PurchasergDAO;
import com.crm.dao.repositoryDAO.InAndOutlogDAO;
import com.crm.dao.repositoryDAO.RepositoryDAO;
import com.crm.dao.saling.ContractDao;
import com.crm.dao.saling.DispatcherDao;
import com.crm.dao.saling.RefundDao;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.Contract;
import com.crm.pojo.Inandoutlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class IOlogService {
    @Autowired
    private InAndOutlogDAO inAndOutlogDAO;
    @Autowired
    private UserInfoDAO userInfoDAO;
    @Autowired
    private ClientInfoDao clientInfoDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private DispatcherDao dispatcherDao;
    @Autowired
    private PurchaseDAO purchaseDAO;
    @Autowired
    private RepositoryDAO repositoryDAO;
    @Autowired
    private PurchasergDAO purchasergDAO;
    @Autowired
    private RefundDao refundDao;


    //添加出入库记录
    public void addIOlog(Inandoutlog inandoutlog){
        inAndOutlogDAO.save(inandoutlog);
    }
    //查询总记录数
    public Long queryCount(){
        return inAndOutlogDAO.count();
    }
    //分页查询所有出入库记录，根据时间排序
    public List<Inandoutlog> allIOlogPaged(int curpage, int pagesize){
        PageRequest pageRequest = new PageRequest(curpage-1,pagesize);
        List<Inandoutlog> list = inAndOutlogDAO.findAllByPaged(pageRequest);
        for (Inandoutlog io:list) {
            //设置经手人
            if(io.getHandmanId()!=null){
                io.setHandman(userInfoDAO.findOne(io.getHandmanId()).getUrealName());
            }else{
                io.setHandman("暂无数据");
            }
            //设置客户/供应商
            //客户
            if(io.getIotype()==1L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(contractDao.findOne(dispatcherDao.findOne(io.getIoDispatherId()).getFkOrderId().longValue()).getFkCustomerId().longValue()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //供应商
            if(io.getIotype()==2L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(purchaseDAO.findOne(io.getIoDispatherId()).getClientId()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //退货入库
            if(io.getIotype()==3L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(refundDao.findOne(io.getIoDispatherId()).getFkRefundCustomerId().longValue()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //退货出库
            if(io.getIotype()==4L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(purchaseDAO.findOne(purchasergDAO.findOne(io.getIoDispatherId()).getPuridfk()).getClientId()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //设置仓库名称
            if(io.getIorid()!=null){
                io.setIorname(repositoryDAO.findOne(io.getIorid()).getRname());
            }else{
                io.setIorname("暂无数据");
            }
        }
        return list;
    }
    //查询已出入库单根据仓库名称或标题模糊 所有内容
    public List<Inandoutlog> findAllIOByrnameOrtitle(String q){
          List<Inandoutlog> list = inAndOutlogDAO.findAllIOByrnameOrtitle("%"+q+"%");
        for (Inandoutlog io:list) {
            //设置经手人
            if(io.getHandmanId()!=null){
                io.setHandman(userInfoDAO.findOne(io.getHandmanId()).getUrealName());
            }else{
                io.setHandman("暂无数据");
            }
            //设置客户/供应商
            //客户
            if(io.getIotype()==1L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(contractDao.findOne(dispatcherDao.findOne(io.getIoDispatherId()).getFkOrderId().longValue()).getFkCustomerId().longValue()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //供应商
            if(io.getIotype()==2L){
                if(io.getIoDispatherId()!=null){
                    io.setClientname(clientInfoDao.findOne(purchaseDAO.findOne(io.getIoDispatherId()).getClientId()).getCname());
                }else{
                    io.setClientname("暂无数据");
                }
            }
            //设置仓库名称
            if(io.getIorid()!=null){
                io.setIorname(repositoryDAO.findOne(io.getIorid()).getRname());
            }else{
                io.setIorname("暂无数据");
            }
        }
        return list;
    }
    //查询已出入库单根据流水id或标题模糊 已入库
    public List<Inandoutlog> findAllIOByrnameOrtitle3(String q){
        List<Inandoutlog> list = inAndOutlogDAO.findAllIOByrnameOrtitle3("%"+q+"%");
        for (Inandoutlog io:list) {
            //设置经手人
            if(io.getHandmanId()!=null){
                io.setHandman(userInfoDAO.findOne(io.getHandmanId()).getUrealName());
            }else{
                io.setHandman("暂无数据");
            }
            //供应商
            if(io.getIoDispatherId()!=null){
                io.setClientname(clientInfoDao.findOne(purchaseDAO.findOne(io.getIoDispatherId()).getClientId()).getCname());
            }else{
                io.setClientname("暂无数据");
            }
            //设置仓库名称
            if(io.getIorid()!=null){
                io.setIorname(repositoryDAO.findOne(io.getIorid()).getRname());
            }else{
                io.setIorname("暂无数据");
            }
        }
        return list;
    }
    //查询已出入库单根据流水id或标题模糊 以出库
    public List<Inandoutlog> findAllIOByrnameOrtitle2(String q){
        List<Inandoutlog> list = inAndOutlogDAO.findAllIOByrnameOrtitle2("%"+q+"%");
        for (Inandoutlog io:list) {
            //设置经手人
            if(io.getHandmanId()!=null){
                io.setHandman(userInfoDAO.findOne(io.getHandmanId()).getUrealName());
            }else{
                io.setHandman("暂无数据");
            }
            //客户
            if(io.getIoDispatherId()!=null){
                io.setClientname(clientInfoDao.findOne(contractDao.findOne(dispatcherDao.findOne(io.getIoDispatherId()).getFkOrderId().longValue()).getFkCustomerId().longValue()).getCname());
            }else{
                io.setClientname("暂无数据");
            }
            //设置仓库名称
            if(io.getIorid()!=null){
                io.setIorname(repositoryDAO.findOne(io.getIorid()).getRname());
            }else{
                io.setIorname("暂无数据");
            }
        }
        return list;
    }
}
